<?php
//创建websocket服务器对象，监听0.0.0.0:9502端口
$ws = new swoole_websocket_server("0.0.0.0", 9502);

//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {    
    $GLOBALS['fd'][] = $request->fd;
	print_r($GLOBALS['fd']);
    //$ws->push($request->fd, "hello, welcome\n");
});

//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {	
    foreach($GLOBALS['fd'] as $key=>$aa){
		//echo $aa."dsda".$frame->fd ;		
        $ws->push($aa,$frame->data);     
    }
   // $ws->push($frame->fd, "server: {$frame->data}");
    // $ws->push($frame->fd, "server: {$frame->data}");
});

//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
	foreach($GLOBALS['fd'] as $key=>$aa){
		if($aa==$fd)
			unset($GLOBALS['fd'][$key]);
    }
});

$ws->start();